﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCPClient
{
    class Program
    {
        static void Main(string[] args)
        {

            Thread nt = new Thread(DoLoop);
            nt.Start();
            //nt.IsBackground = false;
            Thread.Sleep(60000);
            nt.Abort();
        }
        static void DoLoop() {
            IPAddress adr = IPAddress.Parse("192.168.0.130"); //设定通信ip  
            IPEndPoint ipe = new IPEndPoint(adr, 13000); //把ip绑定到ipe上  

            byte[] buffer = new byte[44];

            string hexValues = "f1 c1 01 01 01 0a 0C 02 1B 0C 37 00 0C 02 1B 0D 00 00 00 E9 00 0D 00 02 03 00 09 2C 04 00 00 03";
            string[] hexValuesSplit = hexValues.Split(' ');
            for (int i = 0; i < hexValuesSplit.Length; i++) {
                buffer[i] = Convert.ToByte(hexValuesSplit[i], 16);
            }
            int ii=0;
            while (true) {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);//建立套接字  
                server.Connect(ipe);
                server.Send(buffer);//发送buffer字符  
                //if (server.Receive(buffer) > 0)
                //{
                //    //然后读出buffer的内容  
                //}
                server.Shutdown(SocketShutdown.Both);
                server.Close();
                Console.Write("{0} ", ++ii);
            }
        }
    }
}
